package com.anita.member.application.service;

import com.anita.member.application.dto.MemberProfileResponse;
import com.anita.member.application.port.in.MemberProfileUseCase;
import com.anita.member.common.enums.Resp;
import com.anita.member.common.exception.BusinessException;
import com.anita.member.domain.model.member.*;
import com.anita.member.domain.repository.MemberRepository;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Optional;

/**
 * @author qiu
 * @date 2025/10/20
 * @description
 */
@Slf4j
@Service
@Transactional
public class MemberProfileService implements MemberProfileUseCase {

    @Resource
    private MemberRepository memberRepository;

    /**
     * 根据id查询会员个人信息
     * @param id
     * @return
     */
    @Override
    public MemberProfileResponse getProfile(Long id) {
        Optional<Member> memberOptional = memberRepository.findById(MemberId.of(id));

        if(memberOptional.isEmpty()){
            throw new BusinessException(Resp.USER_NOT_FOUND);
        }

        Member member = memberOptional.get();

        return new MemberProfileResponse(
                member.getId().value(),
                member.getUserName().value(),
                member.getNickName().value(),
                member.getEmail().value(),
                member.getPhone().value(),
                member.getAvatar().value(),
                member.getGender().desc(),
                member.getBirthday(),
                member.getEmailVerified().desc(),
                member.getPhoneVerified().desc(),
                member.getLastLoginAt(),
                member.getLastLoginIp()
        );
    }
}
